package com.company.kotlinlearn.livedata

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.arch.core.util.Function
import androidx.lifecycle.*
import com.company.kotlinlearn.R

private const val TAG = "LiveDataActivity"

class LiveDataActivity : AppCompatActivity() {

    private val testLiveData = MutableLiveData<String>()
    private val testLiveDataInt = MutableLiveData<Int>()

    private val switchLiveData = MutableLiveData<Boolean>()
    private val liveData1 = MutableLiveData<String>()
    private val liveData2 = MutableLiveData<String>()
    private var clickFlag = true

    private lateinit var textView: TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live_data)
        textView = findViewById(R.id.text)
        // liveData使用
//        test()
        // liveData Transformations.Map使用
//        test2()
        // liveData Transformations.Map使用
//        test3()
        // liveData Transformations.switchMap使用
        test4()
    }

    private fun test4() {
        val switchMap =
            Transformations.switchMap(switchLiveData, object : Function<Boolean, LiveData<String>> {
                override fun apply(input: Boolean): LiveData<String> {
                    if (input) {
                        return liveData1
                    }
                    return liveData2
                }
            })
        textView.setOnClickListener(View.OnClickListener {
            liveData1.value = "liveData1 click"
            liveData2.value = "liveData2 click"
            switchLiveData.value = clickFlag
            clickFlag = !clickFlag
        })
//        switchMap.observe(this, Observer {
//            textView.text = it;
//        })
        switchMap.observe(this) {
            textView.text = it
        }
    }

    private fun test3() {
        val map = Transformations.map(
            testLiveData
        ) { input ->
            val a = when (input) {
                "A" -> "学神"
                "B" -> "学霸"
                "C" -> "学生"
                else -> "学渣"
            }
            "评级：$a"
        }
        textView.setOnClickListener {
            testLiveData.value = "A"
        }
        map.observe(this, Observer {
            textView.text = it
        })
    }

    private fun test2() {
        val liveMap = Transformations.map(
            testLiveDataInt
        ) { input ->
            val a = when (input) {
                100 -> input.plus(1)
                101 -> input.plus(1)
                else -> 0
            }
            "testLiveDataInt $a"
        }
        liveMap.observe(this, Observer { textView.text = it })
        textView.setOnClickListener {
            testLiveDataInt.value = 1011
        }
    }

    private fun test() {
        testLiveData.observe(this, Observer<String> {
            Log.d(TAG, "onCreate: update data")
            textView.text = it
        })
        Log.d(TAG, "onCreate: ")
        testLiveData.value = "aaa"
        textView.setOnClickListener {
            //            testLiveData.postValue("bbb")
            //            testLiveData.value = "bbb"
            Handler(Looper.getMainLooper()).postDelayed(Runnable {
                testLiveData.value = "ddd"
                Log.d(TAG, "onCreate: ddddd")
            }, 5000)

        }


        Thread(Runnable {
            Thread.sleep(3000)
            testLiveData.postValue("ccc")
            Log.d(TAG, "onCreate: ccccccc")
        }).start()
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }
}